package com.huawei.petapp2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.huawei.petapp2.R;
import com.huawei.petapp2.entity.Searchhistory;

import java.util.List;

public class HistoryAdapter extends BaseAdapter {

    private ListView listview;
    private Context mContext;//设备上下文
    private List<Searchhistory> mlist;//包含实体类的text
    private ViewHolder mViewHolder;//视图缓冲
    public HistoryAdapter(Context mContext, List<Searchhistory> mlist) {
        this.mContext=mContext;
        this.mlist=mlist;
    }

    @Override
    public int getCount() {//返回list表长度
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {//返回元素项
        Object item=mlist.get(position);
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {//返回元素id
        return position;
    }

    //在外面先定义，ViewHolder静态类
    static class ViewHolder
    {
        public ImageView img;
        public TextView historyName;
        public TextView info;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // History item=mlist.get(position);
        final Searchhistory searchhistory=mlist.get(position);//获取到具体项
        if(convertView==null)//视图不为空再进行创建
        {
            //用layout加载布局，传给布局对象convertView
            //用view找到三个控件，存储在ViewHolder中，再把ViewHolder存到view中
            //完成了把控件展示在listview种的步骤
            convertView = LayoutInflater.from(mContext).inflate(R.layout.history_item_activity,parent,false);
            mViewHolder=new ViewHolder();//初始化
            mViewHolder.historyName=convertView.findViewById(R.id.history_item);
            convertView.setTag(mViewHolder);//把这个对象加到view中
        }else {
            mViewHolder =(ViewHolder) convertView.getTag();
        }
        //为textView中的每一个item设置监听对象
        /*mViewHolder.historyName.setOnClickListener(
                new TextView.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                       String string= mViewHolder.historyName.getText().toString();
                    }
                }

        );*/
        //到具体每一项的实现
        mViewHolder.historyName.setText(searchhistory.getHeyword());
        return convertView;
    }
}
